﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CooStationsToolsNew
{
    /// <summary>
    /// WorkRightAreaUC.xaml 的交互逻辑
    /// </summary>
    public partial class WorkRightAreaUC : UserControl
    {
        
        ModelTools model = new ModelTools();
        Window win;
        public WorkRightAreaUC(Window _win)
        {
            InitializeComponent();
            win = _win;
            imgPBorder.Visibility = Visibility.Collapsed;
            rectPBorder.Visibility = Visibility.Collapsed;
            textPBorder.Visibility = Visibility.Collapsed;
            divPBorder.Visibility = Visibility.Collapsed;
            winPBorder.Visibility = Visibility.Collapsed;
            
        }
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //InitActionCombox();
        }
        public void InitActionCombox()
        {
            clickCB.Items.Clear();
            clickCB.Items.Add("新建事件");
            overCB.Items.Clear();
            overCB.Items.Add("新建事件");
            outCB.Items.Clear();
            outCB.Items.Add("新建事件");
            pageloadCB.Items.Clear();
            pageloadCB.Items.Add("新建事件");

            for (int i = 0; i < App.eventdt.Rows.Count; i++)
            {
                clickCB.Items.Add(App.eventdt.Rows[i][0].ToString());
                overCB.Items.Add(App.eventdt.Rows[i][0].ToString());
                outCB.Items.Add(App.eventdt.Rows[i][0].ToString());
                pageloadCB.Items.Add(App.eventdt.Rows[i][0].ToString());
                if (App.cpage != null)
                {
                    if (App.cpage.OnloadInit == App.eventdt.Rows[i][0].ToString())
                    {
                        pageloadCB.SelectedIndex = i;
                    }
                }
            }
        }
        /// <summary>
        /// 绑定图片
        /// </summary>
        /// <param name="img">图片对象</param>
        public void BindingImg(Image img)
        {
            MainWindow.workleftuc.tabItem3.IsSelected = true;
            imgId.Text = img.Uid;
            System.Windows.Data.Binding bdName = new System.Windows.Data.Binding();
            bdName.Source = imgId;
            bdName.Mode = BindingMode.TwoWay;
            bdName.Path = new PropertyPath("Text");
            img.SetBinding(Image.UidProperty, bdName);

            imgW.Text = img.Width.ToString();
            System.Windows.Data.Binding bdWidth = new System.Windows.Data.Binding();
            bdWidth.Source = imgW;
            bdWidth.Mode = BindingMode.TwoWay;
            bdWidth.StringFormat = "F0";
            bdWidth.Path = new PropertyPath("Text");
            img.SetBinding(Image.WidthProperty, bdWidth);

            imgH.Text = img.Height.ToString();
            System.Windows.Data.Binding bdHeight = new System.Windows.Data.Binding();
            bdHeight.Source = imgH;
            bdHeight.Mode = BindingMode.TwoWay;
            bdHeight.StringFormat = "F0";
            bdHeight.Path = new PropertyPath("Text");
            img.SetBinding(Image.HeightProperty, bdHeight);

            imgUrl.Text = img.Source.ToString().Substring(8);
            System.Windows.Data.Binding bdUrl = new System.Windows.Data.Binding();
            bdUrl.Source = imgUrl;
            bdUrl.Mode = BindingMode.TwoWay;
            bdUrl.Path = new PropertyPath("Text");
            img.SetBinding(Image.SourceProperty, bdUrl);

            alphaSl.Width = 150;
            alphaSl.Maximum = 1;
            alphaSl.Minimum = 0;
            alphaSl.LargeChange = 0.1;
            alphaSl.SmallChange = 0.1;
            //alphaSl.
            if (img.Opacity == 1)
            {
                alphaSl.Value = img.Opacity * 100;
            }
            else
            {
                alphaSl.Value = img.Opacity;
            }
            alphaSl.Value = img.Opacity;
            System.Windows.Data.Binding bdAlphasl = new System.Windows.Data.Binding();
            bdAlphasl.Source = alphaSl;
            bdAlphasl.Mode = BindingMode.TwoWay;
            bdAlphasl.Path = new PropertyPath("Value");
            img.SetBinding(Image.OpacityProperty, bdAlphasl);

            //Canvas imgCan = img.Parent as Canvas;
            imgX.Text = img.GetValue(Canvas.LeftProperty).ToString();
            imgX.Focusable = false;
            System.Windows.Data.Binding bdX = new System.Windows.Data.Binding();
            bdX.Source = imgX;
            bdX.Mode = BindingMode.TwoWay;
            bdX.StringFormat = "F0";
            bdX.Path = new PropertyPath("Text");
            img.SetBinding(Canvas.LeftProperty, bdX);

            imgY.Text = img.GetValue(Canvas.TopProperty).ToString();
            System.Windows.Data.Binding bdY = new System.Windows.Data.Binding();
            bdY.Source = imgY;
            bdY.Mode = BindingMode.TwoWay;
            bdY.StringFormat = "F0";
            bdY.Path = new PropertyPath("Text");
            img.SetBinding(Canvas.TopProperty, bdY);
            imgPBorder.Visibility = Visibility.Visible;
            rectPBorder.Visibility = Visibility.Collapsed;
            textPBorder.Visibility = Visibility.Collapsed;
            divPBorder.Visibility = Visibility.Collapsed;
            winPBorder.Visibility = Visibility.Collapsed;


            Canvas pcan = img.Parent as Canvas;
            WorkMainAreaUC.layer = AdornerLayer.GetAdornerLayer(pcan);
            if (WorkMainAreaUC.myCan != null)
            {
                WorkMainAreaUC.layer.Remove(WorkMainAreaUC.myCan);
            }
            WorkMainAreaUC.myCan = new MyCanvasAdorner(img, true);
            WorkMainAreaUC.layer.Add(WorkMainAreaUC.myCan);
            clickCB.IsEnabled = true;
            overCB.IsEnabled = true;
            outCB.IsEnabled = true;
            CooStationsToolsNew.Model.Pic pic=model.GetPic(img, App.dt);
            if (pic.OnClick != null || pic.OnClick != "")
            {
                clickCB.SelectedIndex = clickCB.Items.IndexOf(pic.OnClick);
            }
            else {
                clickCB.SelectedItem = null;
            }
            if (pic.OnRollOver != null || pic.OnRollOver != "")
            {
                overCB.SelectedIndex = overCB.Items.IndexOf(pic.OnRollOver);
            }
            else
            {
                overCB.SelectedItem = null;
            }
            if (pic.OnRollOut != null || pic.OnRollOut != "")
            {
                outCB.SelectedIndex = outCB.Items.IndexOf(pic.OnRollOut);
            }
            else
            {
                outCB.SelectedItem = null;
            }


            TreeViewItem tvi=  MainWindow.workmenuuc.CheckOTV(MainWindow.workleftuc.treeView1.Items[0] as TreeViewItem, img.Uid);
            tvi.IsSelected = true;
            img.Uid = tvi.Name;
            System.Windows.Data.Binding bdotv = new System.Windows.Data.Binding();
            bdotv.Source = img;
            bdotv.Mode = BindingMode.TwoWay;
            bdotv.StringFormat = "F0";
            bdotv.Path = new PropertyPath("Uid");
            tvi.SetBinding(TreeViewItem.NameProperty, bdotv);

            
        }

        /// <summary>
        /// 取消图片绑定
        /// </summary>
        /// <param name="img">图片对象</param>
        public void DelBindingImg(Image img)
        {
            BindingOperations.ClearAllBindings(img);
            
            img.Uid = imgId.Text;
            TreeViewItem tvi = MainWindow.workmenuuc.CheckOTV(MainWindow.workleftuc.treeView1.Items[0] as TreeViewItem, img.Uid);
            
            BindingOperations.ClearAllBindings(tvi);
            tvi.Name = img.Uid;
            img.Width = double.Parse(imgW.Text);
            img.Height = double.Parse(imgH.Text);
            if (alphaSl.Value == 1)
            {
                img.Opacity = alphaSl.Value * 100;
            }
            else
            {
                img.Opacity = alphaSl.Value;
            }
            img.SetValue(Canvas.LeftProperty, double.Parse(imgX.Text));
            img.SetValue(Canvas.TopProperty, double.Parse(imgY.Text));
            img.SetValue(Image.SourceProperty, new BitmapImage(new Uri(imgUrl.Text.ToString(), UriKind.RelativeOrAbsolute)));

            
        }

        /// <summary>
        /// 绑定矩形
        /// </summary>
        /// <param name="rect">矩形对象</param>
        public void BindingRect(Rectangle rect)
        {
            //name
            rectId.Text = rect.Uid;
            System.Windows.Data.Binding bdName = new System.Windows.Data.Binding();
            bdName.Source = rectId;
            bdName.Mode = BindingMode.TwoWay;
            bdName.Path = new PropertyPath("Text");
            rect.SetBinding(Rectangle.UidProperty, bdName);

            //width
            rectW.Text = rect.Width.ToString();
            System.Windows.Data.Binding bdWidth = new System.Windows.Data.Binding();
            bdWidth.Source = rectW;
            bdWidth.Mode = BindingMode.TwoWay;
            bdWidth.Path = new PropertyPath("Text");
            bdWidth.StringFormat = "F0";
            rect.SetBinding(Rectangle.WidthProperty, bdWidth);
            //height
            rectH.Text = rect.Height.ToString();
            System.Windows.Data.Binding bdHeight = new System.Windows.Data.Binding();
            bdHeight.Source = rectH;
            bdHeight.Mode = BindingMode.TwoWay;
            bdHeight.Path = new PropertyPath("Text");
            bdHeight.StringFormat = "F0";
            rect.SetBinding(Rectangle.HeightProperty, bdHeight);
            //alpha
            rect_alphaSl.Width = 150;
            rect_alphaSl.Maximum = 1;
            rect_alphaSl.Minimum = 0;
            rect_alphaSl.LargeChange = 0.1;
            rect_alphaSl.SmallChange = 0.1;
            //alphaSl.
            if (rect.Opacity == 1)
            {
                rect_alphaSl.Value = rect.Opacity * 100;
            }
            else
            {
                rect_alphaSl.Value = rect.Opacity;
            }
            rect_alphaSl.Value = rect.Opacity;
            System.Windows.Data.Binding bdAlphasl = new System.Windows.Data.Binding();
            bdAlphasl.Source = rect_alphaSl;
            bdAlphasl.Mode = BindingMode.TwoWay;
            bdAlphasl.Path = new PropertyPath("Value");
            rect.SetBinding(Rectangle.OpacityProperty, bdAlphasl);
            //x
            rectX.Text = rect.GetValue(Canvas.LeftProperty).ToString();
            System.Windows.Data.Binding bdX = new System.Windows.Data.Binding();
            bdX.Source = rectX;
            bdX.Mode = BindingMode.TwoWay;
            bdX.Path = new PropertyPath("Text");
            bdX.StringFormat = "F0";
            rect.SetBinding(Canvas.LeftProperty, bdX);
            //y
            rectY.Text = rect.GetValue(Canvas.TopProperty).ToString();
            System.Windows.Data.Binding bdY = new System.Windows.Data.Binding();
            bdY.Source = rectY;
            bdY.Mode = BindingMode.TwoWay;
            bdY.Path = new PropertyPath("Text");
            bdY.StringFormat = "F0";
            rect.SetBinding(Canvas.TopProperty, bdY);
            //color
            //rectColor.Text = "#" + rect.GetValue(Rectangle.FillProperty).ToString().Substring(3);
            rectColorShow.Text = rect.GetValue(Rectangle.FillProperty).ToString();
            rectColorP.SelectedColor = (System.Windows.Media.Color)(System.Windows.Media.ColorConverter.ConvertFromString(rect.GetValue(Rectangle.FillProperty).ToString()));
            //rectColor.Tag = rect.GetValue(Rectangle.FillProperty).ToString();
            System.Windows.Data.Binding bdColor = new System.Windows.Data.Binding();
            bdColor.Source = rectColorShow;
            bdColor.Mode = BindingMode.TwoWay;
            bdColor.Path = new PropertyPath("Text");
            rect.SetBinding(Rectangle.FillProperty, bdColor);
            //border
            rectBorder.Text = rect.GetValue(Rectangle.StrokeThicknessProperty).ToString();
            System.Windows.Data.Binding bdBorder = new System.Windows.Data.Binding();
            bdBorder.Source = rectBorder;
            bdBorder.Mode = BindingMode.TwoWay;
            bdBorder.Path = new PropertyPath("Text");
            bdBorder.StringFormat = "F0";
            rect.SetBinding(Rectangle.StrokeThicknessProperty, bdBorder);


            //bordercolor

            //rectBColor.Text = "#" + rect.GetValue(Rectangle.StrokeProperty).ToString().Substring(3);
            rectBColorShow.Text = rect.GetValue(Rectangle.StrokeProperty).ToString();
            rectbColorP.SelectedColor = (System.Windows.Media.Color)(System.Windows.Media.ColorConverter.ConvertFromString(rect.GetValue(Rectangle.StrokeProperty).ToString()));
            // rectbColorP.SelectedColor = ToMediaColor(rectBColorShow.Text);
            System.Windows.Data.Binding bdBColor = new System.Windows.Data.Binding();
            bdBColor.Source = rectBColorShow;
            bdBColor.Mode = BindingMode.TwoWay;
            bdBColor.Path = new PropertyPath("Text");
            rect.SetBinding(Rectangle.StrokeProperty, bdBColor);
            imgPBorder.Visibility = Visibility.Collapsed;
            rectPBorder.Visibility = Visibility.Visible;
            textPBorder.Visibility = Visibility.Collapsed;
            divPBorder.Visibility = Visibility.Collapsed;
            winPBorder.Visibility = Visibility.Collapsed;

            Canvas pcan = rect.Parent as Canvas;
            WorkMainAreaUC.layer = AdornerLayer.GetAdornerLayer(pcan);
            if (WorkMainAreaUC.myCan != null)
            {
                WorkMainAreaUC.layer.Remove(WorkMainAreaUC.myCan);
            }
            WorkMainAreaUC.myCan = new MyCanvasAdorner(rect, true);
            WorkMainAreaUC.layer.Add(WorkMainAreaUC.myCan);

            clickCB.IsEnabled = true;
            overCB.IsEnabled = true;
            outCB.IsEnabled = true;
            CooStationsToolsNew.Model.Rect rectangle = model.GetRect(rect, App.dt);
            if (rectangle.OnClick != null || rectangle.OnClick != "")
            {
                clickCB.SelectedIndex = clickCB.Items.IndexOf(rectangle.OnClick);
            }
            else
            {
                clickCB.SelectedItem = null;
            }
            if (rectangle.OnRollOver != null || rectangle.OnRollOver != "")
            {
                overCB.SelectedIndex = overCB.Items.IndexOf(rectangle.OnRollOver);
            }
            else
            {
                overCB.SelectedItem = null;
            }
            if (rectangle.OnRollOut != null || rectangle.OnRollOut != "")
            {
                outCB.SelectedIndex = outCB.Items.IndexOf(rectangle.OnRollOut);
            }
            else
            {
                outCB.SelectedItem = null;
            }
            TreeViewItem tvi = MainWindow.workmenuuc.CheckOTV(MainWindow.workleftuc.treeView1.Items[0] as TreeViewItem, rect.Uid);
            rect.Uid = tvi.Name;
            System.Windows.Data.Binding bdotv = new System.Windows.Data.Binding();
            bdotv.Source = rect;
            bdotv.Mode = BindingMode.TwoWay;
            bdotv.StringFormat = "F0";
            bdotv.Path = new PropertyPath("Uid");
            tvi.SetBinding(TreeViewItem.NameProperty, bdotv);
        }

        /// <summary>
        /// 取消矩形绑定
        /// </summary>
        /// <param name="rect">矩形对象</param>
        public void DelBindingRect(Rectangle rect)
        {
            BindingOperations.ClearAllBindings(rect);
            rect.Uid = rectId.Text;
            TreeViewItem tvi = MainWindow.workmenuuc.CheckOTV(MainWindow.workleftuc.treeView1.Items[0] as TreeViewItem, rect.Uid);
            BindingOperations.ClearAllBindings(tvi);
            tvi.Name = rect.Uid;
            rect.Width = double.Parse(rectW.Text);
            rect.Height = double.Parse(rectH.Text);
            if (rect_alphaSl.Value == 1)
            {
                rect.Opacity = rect_alphaSl.Value * 100;
            }
            else
            {
                rect.Opacity = rect_alphaSl.Value;
            }
            rect.SetValue(Canvas.LeftProperty, double.Parse(rectX.Text));
            rect.SetValue(Canvas.TopProperty, double.Parse(rectY.Text));
            rect.Fill = new SolidColorBrush(ModelTools.ToColor(rectColorShow.Text));
            System.Windows.Data.Binding bdBColor = new System.Windows.Data.Binding();
            bdBColor.Source = null;
            bdBColor.Mode = BindingMode.TwoWay;
            bdBColor.Path = new PropertyPath("Text");
            rect.SetBinding(Rectangle.StrokeProperty, bdBColor);
            rect.Stroke = new SolidColorBrush(ModelTools.ToColor(rectBColorShow.Text));
            rect.StrokeThickness = double.Parse(rectBorder.Text);
        }


        /// <summary>
        /// 绑定div
        /// </summary>
        /// <param name="rect">div对象</param>
        public void BindingDiv(Canvas can)
        {
            //name
            divId.Text = can.Uid;
            System.Windows.Data.Binding bdName = new System.Windows.Data.Binding();
            bdName.Source = divId;
            bdName.Mode = BindingMode.TwoWay;
            bdName.Path = new PropertyPath("Text");
            can.SetBinding(Canvas.UidProperty, bdName);
            //width
            divW.Text = can.Width.ToString();
            System.Windows.Data.Binding bdWidth = new System.Windows.Data.Binding();
            bdWidth.Source = divW;
            bdWidth.Mode = BindingMode.TwoWay;
            bdWidth.Path = new PropertyPath("Text");
            bdWidth.StringFormat = "F0";
            can.SetBinding(Canvas.WidthProperty, bdWidth);
            //height
            divH.Text = can.Height.ToString();
            System.Windows.Data.Binding bdHeight = new System.Windows.Data.Binding();
            bdHeight.Source = divH;
            bdHeight.Mode = BindingMode.TwoWay;
            bdHeight.Path = new PropertyPath("Text");
            bdHeight.StringFormat = "F0";
            can.SetBinding(Canvas.HeightProperty, bdHeight);
            //alpha
            divalphaSl.Width = 150;
            divalphaSl.Maximum = 1;
            divalphaSl.Minimum = 0;
            divalphaSl.LargeChange = 0.1;
            divalphaSl.SmallChange = 0.1;
            //alphaSl.
            if (can.Opacity == 1)
            {
                divalphaSl.Value = can.Opacity * 100;
            }
            else
            {
                divalphaSl.Value = can.Opacity;
            }
            divalphaSl.Value = can.Opacity;
            //rect_alphaTb.Text = rect.Opacity.ToString();
            System.Windows.Data.Binding bdAlphasl = new System.Windows.Data.Binding();
            bdAlphasl.Source = divalphaSl;
            bdAlphasl.Mode = BindingMode.TwoWay;
            bdAlphasl.Path = new PropertyPath("Value");
            can.SetBinding(Canvas.OpacityProperty, bdAlphasl);
            //x
            divX.Text = can.GetValue(Canvas.LeftProperty).ToString();
            System.Windows.Data.Binding bdX = new System.Windows.Data.Binding();
            bdX.Source = divX;
            bdX.Mode = BindingMode.TwoWay;
            bdX.Path = new PropertyPath("Text");
            bdX.StringFormat = "F0";
            can.SetBinding(Canvas.LeftProperty, bdX);
            //y
            divY.Text = can.GetValue(Canvas.TopProperty).ToString();
            System.Windows.Data.Binding bdY = new System.Windows.Data.Binding();
            bdY.Source = divY;
            bdY.Mode = BindingMode.TwoWay;
            bdY.Path = new PropertyPath("Text");
            bdY.StringFormat = "F0";
            can.SetBinding(Canvas.TopProperty, bdY);
            imgPBorder.Visibility = Visibility.Collapsed;
            rectPBorder.Visibility = Visibility.Collapsed;
            textPBorder.Visibility = Visibility.Collapsed;
            divPBorder.Visibility = Visibility.Visible;
            winPBorder.Visibility = Visibility.Collapsed;

            Canvas pcan = can.Parent as Canvas;
            WorkMainAreaUC.layer = AdornerLayer.GetAdornerLayer(pcan);
            if (WorkMainAreaUC.myCan != null)
            {
                WorkMainAreaUC.layer.Remove(WorkMainAreaUC.myCan);
            }
            WorkMainAreaUC.myCan = new MyCanvasAdorner(can, true);
            WorkMainAreaUC.layer.Add(WorkMainAreaUC.myCan);

            clickCB.IsEnabled = false;
            clickCB.SelectedItem = null;
            overCB.IsEnabled = false;
            overCB.SelectedItem = null;
            outCB.IsEnabled = false;
            outCB.SelectedItem = null;

            TreeViewItem tvi = MainWindow.workmenuuc.CheckOTV(MainWindow.workleftuc.treeView1.Items[0] as TreeViewItem, can.Uid);
            can.Uid = tvi.Name;
            System.Windows.Data.Binding bdotv = new System.Windows.Data.Binding();
            bdotv.Source = can;
            bdotv.Mode = BindingMode.TwoWay;
            bdotv.StringFormat = "F0";
            bdotv.Path = new PropertyPath("Uid");
            tvi.SetBinding(TreeViewItem.NameProperty, bdotv);

        }
        public void DelBindingAll()
        {
            try
            {
                DelBindingRect(App.uiobj as Rectangle);
            }
            catch { }
            try
            {
                DelBindingImg(App.uiobj as Image);
            }
            catch { }
            try
            {
                DelBindingDiv(App.uiobj as Canvas);
            }
            catch { }
            try
            {
                DelBindingText(App.uiobj as TextBlock);
            }
            catch { }
            App.uilockobj = null;
            App.uiobj = null;
            ShowPBorder("win");
            //初始化ModelTools的值
            model.ClearObj();
        }

        public void ShowPBorder(string type)
        {
            if (type == "image")
            {
                rectPBorder.Visibility = Visibility.Collapsed;
                winPBorder.Visibility = Visibility.Collapsed;
                imgPBorder.Visibility = Visibility.Visible;
                divPBorder.Visibility = Visibility.Collapsed;
                textPBorder.Visibility = Visibility.Collapsed;
            }
            else if (type == "rect")
            {
                rectPBorder.Visibility = Visibility.Visible;
                winPBorder.Visibility = Visibility.Collapsed;
                imgPBorder.Visibility = Visibility.Collapsed;
                divPBorder.Visibility = Visibility.Collapsed;
                textPBorder.Visibility = Visibility.Collapsed;
            }
            else if (type == "div")
            {
                rectPBorder.Visibility = Visibility.Collapsed;
                winPBorder.Visibility = Visibility.Collapsed;
                imgPBorder.Visibility = Visibility.Collapsed;
                divPBorder.Visibility = Visibility.Visible;
                textPBorder.Visibility = Visibility.Collapsed;
            }
            else if (type == "text")
            {
                rectPBorder.Visibility = Visibility.Collapsed;
                winPBorder.Visibility = Visibility.Collapsed;
                imgPBorder.Visibility = Visibility.Collapsed;
                divPBorder.Visibility = Visibility.Collapsed;
                textPBorder.Visibility = Visibility.Visible;
            }
            else if (type == "win")
            {
                rectPBorder.Visibility = Visibility.Collapsed;
                winPBorder.Visibility = Visibility.Visible;
                imgPBorder.Visibility = Visibility.Collapsed;
                divPBorder.Visibility = Visibility.Collapsed;
                textPBorder.Visibility = Visibility.Collapsed;
            }
        }

        /// <summary>
        /// 取消div绑定
        /// </summary>
        /// <param name="rect">div对象</param>
        public void DelBindingDiv(Canvas can)
        {
            BindingOperations.ClearAllBindings(can);
            can.Uid = divId.Text;
            TreeViewItem tvi = MainWindow.workmenuuc.CheckOTV(MainWindow.workleftuc.treeView1.Items[0] as TreeViewItem, can.Uid);
            BindingOperations.ClearAllBindings(tvi);
            tvi.Name = can.Uid;
            can.Width = double.Parse(divW.Text);
            can.Height = double.Parse(divH.Text);
            if (divalphaSl.Value == 1)
            {
                can.Opacity = divalphaSl.Value * 100;
            }
            else
            {
                can.Opacity = divalphaSl.Value;
            }
            can.SetValue(Canvas.LeftProperty, double.Parse(divX.Text));
            can.SetValue(Canvas.TopProperty, double.Parse(divY.Text));
        }

        /// <summary>
        /// 绑定文本
        /// </summary>
        /// <param name="rect">文本对象</param>
        public void BindingText(TextBlock textB)
        {
            textB.SizeChanged += new SizeChangedEventHandler(textB_SizeChanged);
            //name
            textId.Text = textB.Uid;
            System.Windows.Data.Binding bdName = new System.Windows.Data.Binding();
            bdName.Source = textId;
            bdName.Mode = BindingMode.TwoWay;
            bdName.Path = new PropertyPath("Text");
            textB.SetBinding(TextBlock.UidProperty, bdName);
            //value
            textValue.Text = textB.Text;
            System.Windows.Data.Binding bdValue = new System.Windows.Data.Binding();
            bdValue.Source = textValue;
            bdValue.Mode = BindingMode.TwoWay;
            bdValue.Path = new PropertyPath("Text");
            bdValue.StringFormat = "F0";
            textB.SetBinding(TextBlock.TextProperty, bdValue);
        
            //x
            textX.Text = textB.GetValue(Canvas.LeftProperty).ToString();
            System.Windows.Data.Binding bdX = new System.Windows.Data.Binding();
            bdX.Source = textX;
            bdX.Mode = BindingMode.TwoWay;
            bdX.Path = new PropertyPath("Text");
            bdX.StringFormat = "F0";
            textB.SetBinding(Canvas.LeftProperty, bdX);
            //y
            textY.Text = textB.GetValue(Canvas.TopProperty).ToString();
            System.Windows.Data.Binding bdY = new System.Windows.Data.Binding();
            bdY.Source = textY;
            bdY.Mode = BindingMode.TwoWay;
            bdY.Path = new PropertyPath("Text");
            bdY.StringFormat = "F0";
            textB.SetBinding(Canvas.TopProperty, bdY);
            //Bold

            if (textB.FontWeight == FontWeights.Bold)
            {
                bCheckBox.IsChecked = true;

            }
            else if (textB.FontWeight == FontWeights.Normal)
            {
                bCheckBox.IsChecked = false;

            }
            //align
            if (textB.TextAlignment == TextAlignment.Left)
            {
                leftRBtn.IsChecked = true;

            }
            else if (textB.TextAlignment == TextAlignment.Right)
            {
                rightRBtn.IsChecked = true;

            }
            else if (textB.TextAlignment == TextAlignment.Center)
            {
                centerRBtn.IsChecked = true;

            }
            //Mulitline
            if (textB.TextWrapping == TextWrapping.Wrap)
            {
                textMulitlineCb.SelectedIndex = 0;
            }
            else
            {
                textMulitlineCb.SelectedIndex = 1;
            }
         
            //color
            //textColor.Text = "#" + textB.GetValue(TextBlock.ForegroundProperty).ToString().Substring(3);
            textColorShow.Text = textB.GetValue(TextBlock.ForegroundProperty).ToString();
            textColorP.SelectedColor = (System.Windows.Media.Color)(System.Windows.Media.ColorConverter.ConvertFromString(textB.GetValue(TextBlock.ForegroundProperty).ToString()));
            System.Windows.Data.Binding bdColor = new System.Windows.Data.Binding();
            bdColor.Source = textColorShow;
            bdColor.Mode = BindingMode.TwoWay;
            bdColor.Path = new PropertyPath("Text");
            textB.SetBinding(TextBlock.ForegroundProperty, bdColor);
            //size
            textSize.Text = textB.GetValue(TextBlock.FontSizeProperty).ToString();
            System.Windows.Data.Binding bdSize = new System.Windows.Data.Binding();
            bdSize.Source = textSize;
            bdSize.Mode = BindingMode.TwoWay;
            bdSize.Path = new PropertyPath("Text");
            textB.SetBinding(TextBlock.FontSizeProperty, bdSize);
            imgPBorder.Visibility = Visibility.Collapsed;
            rectPBorder.Visibility = Visibility.Collapsed;
            textPBorder.Visibility = Visibility.Visible;
            divPBorder.Visibility = Visibility.Collapsed;
            winPBorder.Visibility = Visibility.Collapsed;

            

            Canvas pcan = textB.Parent as Canvas;
            WorkMainAreaUC.layer = AdornerLayer.GetAdornerLayer(pcan);
            if (WorkMainAreaUC.myCan != null)
            {
                WorkMainAreaUC.layer.Remove(WorkMainAreaUC.myCan);
            }
            WorkMainAreaUC.myCan = new MyCanvasAdorner(textB,false);
            WorkMainAreaUC.layer.Add(WorkMainAreaUC.myCan);

            clickCB.IsEnabled = false;
            clickCB.SelectedItem = null;
            overCB.IsEnabled = false;
            overCB.SelectedItem = null;
            outCB.IsEnabled = false;
            outCB.SelectedItem = null;

            TreeViewItem tvi = MainWindow.workmenuuc.CheckOTV(MainWindow.workleftuc.treeView1.Items[0] as TreeViewItem, textB.Uid);
            textB.Uid = tvi.Name;
            System.Windows.Data.Binding bdotv = new System.Windows.Data.Binding();
            bdotv.Source = textB;
            bdotv.Mode = BindingMode.TwoWay;
            bdotv.StringFormat = "F0";
            bdotv.Path = new PropertyPath("Uid");
            tvi.SetBinding(TreeViewItem.NameProperty, bdotv);
        }

        void textB_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            TextBlock tb = sender as TextBlock;
            textW.Text = tb.ActualWidth.ToString("F0");
            textH.Text = tb.ActualHeight.ToString("F0");
        }

        /// <summary>
        /// 取消Text绑定
        /// </summary>
        /// <param name="rect">Text对象</param>
        public void DelBindingText(TextBlock textB)
        {
            BindingOperations.ClearAllBindings(textB);
            textB.Uid = textId.Text;
            TreeViewItem tvi = MainWindow.workmenuuc.CheckOTV(MainWindow.workleftuc.treeView1.Items[0] as TreeViewItem, textB.Uid);
            BindingOperations.ClearAllBindings(tvi);
            tvi.Name = textB.Uid;
            textB.Text = textValue.Text;
            textB.SetValue(Canvas.LeftProperty, double.Parse(textX.Text));
            textB.SetValue(Canvas.TopProperty, double.Parse(textY.Text));
            textB.SetValue(TextBlock.FontSizeProperty, double.Parse(textSize.Text));
            textB.Foreground = new SolidColorBrush(ModelTools.ToColor(textColorShow.Text));
            //Bold
            if (bCheckBox.IsChecked == true)
            {
                textB.FontWeight = FontWeights.Bold;
            }
            else if (bCheckBox.IsChecked == false)
            {
                textB.FontWeight = FontWeights.Normal;
            }
            //align
            if (leftRBtn.IsChecked == true)
            {
                textB.TextAlignment = TextAlignment.Left;
            }
            else if (rightRBtn.IsChecked == true)
            {
                textB.TextAlignment = TextAlignment.Right;
            }
            else if (centerRBtn.IsChecked == true)
            {
                textB.TextAlignment = TextAlignment.Center;
            }
            //Mulitline
            if (textMulitlineCb.SelectedIndex == 0)
            {
                textB.TextWrapping = TextWrapping.Wrap;
            }
            else
            {
                textB.TextWrapping = TextWrapping.NoWrap;
            }
        }


        private void righttabItem2_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void rightbtn_hidden_Click(object sender, RoutedEventArgs e)
        {

        }

        private void imgId_PreviewKeyDown(object sender, KeyEventArgs e)
        {

        }

        private void imgId_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void imgId_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {

        }

        private void imgX_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void imgX_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {

        }

        private void imgX_PreviewKeyDown(object sender, KeyEventArgs e)
        {

        }

        private void imgY_PreviewKeyDown(object sender, KeyEventArgs e)
        {

        }

        private void imgY_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void imgY_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {

        }

        private void imgW_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {

        }

        private void imgW_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void imgW_PreviewKeyDown(object sender, KeyEventArgs e)
        {

        }

        private void imgH_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {

        }

        private void imgH_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void imgH_PreviewKeyDown(object sender, KeyEventArgs e)
        {

        }

        private void alphaSl_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {

        }

        private void alphaSl_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            alphaTb.Text = (Convert.ToInt32(e.NewValue * 100)).ToString();
        }

        private void alphaTb_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                Keyboard.ClearFocus();
            }
        }

        private void alphaTb_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            Keyboard.Focus(rect_alphaTb);
        }

        private void alphaTb_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            short num;
            if (alphaTb.Text == "" || !Int16.TryParse(alphaTb.Text, out num))
            {
                MessageForm1 mf1 = new MessageForm1("有非法输入");
                mf1.Owner = win;
                mf1.ShowDialog();
                alphaTb.Undo();
            }
            else if (!model.SaveToImageData(App.uiobj as Image, App.dt))
            {

                MessageForm1 mf1 = new MessageForm1("有非法输入");
                mf1.Owner = win;
                mf1.ShowDialog();
                alphaTb.Text = alphaTb.Text.Trim();
                alphaTb.Undo();
            }
        }

        private void alphaTb_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                alphaSl.Value = double.Parse(alphaTb.Text) / 100;
            }
            catch { }
        }

        private void rectId_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {

        }

        private void rectId_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void rectId_PreviewKeyDown(object sender, KeyEventArgs e)
        {

        }

        private void rectX_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {

        }

        private void rectX_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void rectX_PreviewKeyDown(object sender, KeyEventArgs e)
        {

        }

        private void rectY_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {

        }

        private void rectY_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void rectY_PreviewKeyDown(object sender, KeyEventArgs e)
        {

        }

        private void rectW_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {

        }

        private void rectW_PreviewKeyDown(object sender, KeyEventArgs e)
        {

        }

        private void rectW_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void rectH_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {

        }

        private void rectH_PreviewKeyDown(object sender, KeyEventArgs e)
        {

        }

        private void rectH_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void rectColorP_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs<Color> e)
        {
            rectColorShow.Text = rectColorP.SelectedColor.ToString();
            model.SaveToRectData(App.uiobj as Rectangle, App.dt);
        }

        private void rectBorder_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {

        }

        private void rectBorder_PreviewKeyDown(object sender, KeyEventArgs e)
        {

        }

        private void rectBorder_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void rectbColorP_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs<Color> e)
        {
            rectBColorShow.Text = rectbColorP.SelectedColor.ToString();
            model.SaveToRectData(App.uiobj as Rectangle, App.dt);
        }

        private void bCheckBox_Click(object sender, RoutedEventArgs e)
        {

        }

        private void bCheckBox_LostFocus(object sender, RoutedEventArgs e)
        {

        }

        private void textSize_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {

        }

        private void textSize_PreviewKeyDown(object sender, KeyEventArgs e)
        {

        }

        private void textSize_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void textSize_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void textColorP_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs<Color> e)
        {
            textColorShow.Text = textColorP.SelectedColor.ToString();
            model.SaveToTextData(App.uiobj as TextBlock, App.dt);
        }

        private void textMulitlineCb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
        private void clickEditBtn_Click(object sender, RoutedEventArgs e)
        {

        }

        private void overEditBtn_Click(object sender, RoutedEventArgs e)
        {

        }

        private void overDelBtn_Click(object sender, RoutedEventArgs e)
        {

        }

        private void loadDelBtn_Click(object sender, RoutedEventArgs e)
        {

        }

        private void loadEditBtn_Click(object sender, RoutedEventArgs e)
        {

        }

        private void outDelBtn_Click(object sender, RoutedEventArgs e)
        {

        }

        private void outEditBtn_Click(object sender, RoutedEventArgs e)
        {

        }

        private void clickDelBtn_Click(object sender, RoutedEventArgs e)
        {

        }

        private void centerRBtn_Click(object sender, RoutedEventArgs e)
        {

        }

        private void clickCB_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
         //  CooStationsToolsNew.Model.Pic pic = model.GetPic(App.uiobj as Image, App.dt);
           // clickCB.SelectedValuePath = imgAC.Uid + "_onClick";
           // pic.OnClick = clickCB.SelectedValue.ToString();
        }

        private void overCB_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void outCB_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void pageloadCB_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void divalphaSl_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {

        }

        private void divalphaTb_PreviewKeyDown(object sender, KeyEventArgs e)
        {

        }

        private void divalphaSl_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {

        }

        private void divalphaTb_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {

        }

        private void textY_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void textW_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {

        }

        private void textW_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void textW_PreviewKeyDown(object sender, KeyEventArgs e)
        {

        }

        private void textH_PreviewKeyDown(object sender, KeyEventArgs e)
        {

        }

        private void textH_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void textId_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {

        }

        private void textValue_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {

        }

        private void textValue_PreviewKeyDown(object sender, KeyEventArgs e)
        {

        }

        private void textId_PreviewKeyDown(object sender, KeyEventArgs e)
        {

        }

        private void divalphaTb_PreviewKeyDown_1(object sender, KeyEventArgs e)
        {

        }

        private void divalphaTb_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void divalphaTb_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void divH_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {

        }

        private void divH_PreviewKeyDown(object sender, KeyEventArgs e)
        {

        }

        private void divH_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void textY_PreviewKeyDown(object sender, KeyEventArgs e)
        {

        }

        private void textW_LostKeyboardFocus_1(object sender, KeyboardFocusChangedEventArgs e)
        {

        }

        private void textX_PreviewKeyDown(object sender, KeyEventArgs e)
        {

        }

        private void textX_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void textX_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {

        }

        private void divId_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {

        }

        private void divId_PreviewKeyDown(object sender, KeyEventArgs e)
        {

        }

        private void divId_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void divW_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {

        }

        private void divW_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void divW_PreviewKeyDown(object sender, KeyEventArgs e)
        {

        }

        private void divX_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {

        }

        private void divX_PreviewKeyDown(object sender, KeyEventArgs e)
        {

        }

        private void divX_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void divY_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {

        }

        private void divY_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void divY_PreviewKeyDown(object sender, KeyEventArgs e)
        {

        }

        private void leftRBtn_Click(object sender, RoutedEventArgs e)
        {

        }

        private void rect_alphaSl_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {

        }

        private void rect_alphaSl_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            rect_alphaTb.Text = (Convert.ToInt32(e.NewValue * 100)).ToString();
        }

        private void rect_alphaTb_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            short num;
            if (rect_alphaTb.Text == "" || !Int16.TryParse(rect_alphaTb.Text, out num))
            {
                MessageForm1 mf1 = new MessageForm1("有非法输入");
                mf1.Owner = win;
                mf1.ShowDialog();
                rect_alphaTb.Undo();
            }
            else if (!model.SaveToRectData(App.uiobj as Rectangle, App.dt))
            {
                MessageForm1 mf1 = new MessageForm1("有非法输入");
                mf1.Owner = win;
                mf1.ShowDialog();
                rect_alphaTb.Text = rect_alphaTb.Text.Trim();
                rect_alphaTb.Undo();
            }
        }

        private void rect_alphaTb_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                Keyboard.ClearFocus();
            }
        }

        private void rect_alphaTb_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
                Keyboard.Focus(rect_alphaTb);
        }

        private void rect_alphaTb_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                rect_alphaSl.Value = double.Parse(rect_alphaTb.Text) / 100;
            }
            catch { }
        }

        private void right2btn_hidden_Click(object sender, RoutedEventArgs e)
        {

        }

        private void rightRBtn_Click(object sender, RoutedEventArgs e)
        {

        }

        private void rtabItem3_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void textMulitlineCb_LostFocus(object sender, RoutedEventArgs e)
        {

        }

        private void winWidth_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void winHeight_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void winWidth_PreviewKeyDown(object sender, KeyEventArgs e)
        {

        }

        private void winHeight_PreviewKeyDown(object sender, KeyEventArgs e)
        {

        }

        private void textId_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void textValue_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void textY_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {

        } 
       
    }
}
